package com.bjbn.controller;

import com.bjbn.bean.cache.Cache;
import com.bjbn.bean.entity.BaseStand;
import com.bjbn.bean.entity.FltInfoDynamic;
import com.bjbn.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
@Slf4j
public class IndexController {

    @Autowired
    AirportService airportService;

    @Autowired
    FlightAirlineService flightAirlineService;

    @Autowired
    StandService standService;

    @Autowired
    private FltInfoDynamicService fltInfoDynamicService;

    @Autowired
    private FlightInfoService flightInfoService;

    @RequestMapping("/hello")
    public String index() {
        return "Hello World!";
    }

    @RequestMapping("/airport")
    public String airport() {
        log.info("getAllAirport");
        return airportService.getAllAirport().toString();
    }

    @RequestMapping("/flight")
    public String flight() {
        log.info("getFlightInfoByFlid");
        return flightAirlineService.getFlightInfoByFlid("44549898").toString();
    }

    @RequestMapping("/stand")
    public String stand() {
        log.info("getAllStand");
        return standService.getAllStand().toString();
    }

    @RequestMapping("/stand-cache")
    public String standCache() {
        log.info("getStandCache");
        StringBuilder result = new StringBuilder();
        result.append("机位缓存总数: ").append(Cache.baseStandMap.size()).append("\n");
        
        // 显示前5个机位信息
        int count = 0;
        for (BaseStand stand : Cache.baseStandMap.values()) {
            if (count >= 5) break;
            result.append("机位编号: ").append(stand.getCode())
                  .append(", 中文描述: ").append(stand.getCnnm())
                  .append(", 是否远机位: ").append(stand.getIsRemoteApron())
                  .append("\n");
            count++;
        }
        return result.toString();
    }

    @RequestMapping("/test")
    public String test() {
        String message = "{\"FLID\":\"47432128\"}";
        FltInfoDynamic flightInfo = FlightHandle.handleFltAirline(message, flightAirlineService);
        if (null != flightInfo) {
            log.debug("FlightInfo: {}", flightInfo);
            fltInfoDynamicService.saveOrUpdateFltInfoDynamic(flightInfo);
        }
        return "success";
    }

    @RequestMapping("/test-stand-contact")
    public String testStandContact() {
        log.info("testStandContact");
        StringBuilder result = new StringBuilder();
        result.append("测试机位STND_CONTACT计算逻辑:\n");
        
        // 测试几个示例机位
        String[] testStands = {"A01", "B02", "C03", "D04", "E05"};
        
        for (String standCode : testStands) {
            BaseStand baseStand = Cache.baseStandMap.get(standCode);
            if (baseStand != null) {
                String isRemoteApron = baseStand.getIsRemoteApron();
                String standContact = "N"; // 默认不靠桥（远机位）
                
                if ("N".equals(isRemoteApron)) {
                    standContact = "Y"; // 近机位，靠桥
                } else {
                    standContact = "N"; // 远机位，不靠桥
                }
                
                result.append("机位: ").append(standCode)
                      .append(", ISREMOTEAPRON: ").append(isRemoteApron)
                      .append(", STND_CONTACT: ").append(standContact)
                      .append("\n");
            } else {
                result.append("机位: ").append(standCode).append(" 在缓存中未找到\n");
            }
        }
        return result.toString();
    }
}
